package GTGExpession;

import java.util.*;

/**
 * Class for keeping expression as a list of AND statements
 * ENG: Product contains simple variables
 * PL:  Iloczyn sklada sie ze zmiennych
 * @author Tomasz Werszko
 * @author Rafal Magda
 */
public class GTGAnd {
	/**
	 * Comparator needs sorted variables inside ands to work correctly
	 */
	public static class GTGAndStringComparator implements Comparator<GTGAnd>{
		public int compare(GTGAnd and1, GTGAnd and2){
			if(and1 == and2){
				return 0;
			}
			if(and1 == null){
				return -1;
			}
			if(and2 == null){
				return 1;
			}
			if(and1.variables.size() == 0 && and2.variables.size() == 0){
				return 0;
			}else if(and1.variables.size() == 0){
				return -1;
			}else if(and2.variables.size() == 0){
				return 1;
			}else{
				int i = 0;
				int compare = 0;
				while(compare == 0){
					compare = and1.variables.get(i).compareTo(and2.variables.get(i));
					i++;
					if(i > and1.variables.size()-1 || i > and2.variables.size()-1){
						if(compare == 0){
							if(and1.variables.size() < and2.variables.size()){
								return -1;
							}else if(and1.variables.size() > and2.variables.size()){
								return 1;
							}else{
								return 0;
							}
						}else{
							return compare;
						}
					}
				}
				return compare;
			}
		}
		
	}
	
	public ArrayList<String> variables = new ArrayList<String>();

	/**
	 * Constructs GTGAnd based on input string;
	 * String should be received only from GTGOr class.
	 * @param s_or input string generated by GTGOr class
	 */
	public GTGAnd(String s_or) {
		variables = new ArrayList<String>();
		
		int orBegin = 0, orEnd = s_or.length(), orCurEnd = 0;
		
		//checking for special characters
		//we consider only string before special character
		if(s_or.contains("+")){
			orEnd = s_or.indexOf("+");
		}
		if(s_or.contains("-") && s_or.indexOf("-") < orEnd){
			orEnd = s_or.indexOf("-");
		}
		if(s_or.contains("^") && s_or.indexOf("^") < orEnd){
			orEnd = s_or.indexOf("^");
		}
		if(s_or.contains("/") && s_or.indexOf("/") < orEnd){
			orEnd = s_or.indexOf("/");
		}
		
		//getting variables
		while(true){
			orCurEnd = s_or.indexOf("*", orBegin);
			if(orCurEnd < 0 || (orEnd > 0 && orCurEnd > orEnd)){
				//variable after last "*"
				orCurEnd = orEnd;
			}
			String s_and = s_or.substring(orBegin, orCurEnd);
			//trimming, cutting, etc.
			s_and = s_and.replace("(", "");
			s_and = s_and.replace(")", "");
			s_and = s_and.trim();
			s_and = s_and.toUpperCase();
			if(s_and.length() > 0){
				variables.add(new String(s_and));
			}
			if(orCurEnd == orEnd){
				break;
			}
			orBegin=orCurEnd + 1;
		}
		//sorting
		Collections.sort(variables);
	}
	
	public GTGAnd() {
		variables = new ArrayList<String>();
	}
	
	/**
	 * Returns string representation of this object
	 */
	public String toString(){
		String result = new String();
		
		Iterator<String> iterator = this.variables.iterator();
		while(iterator.hasNext()){
			String string = iterator.next();
			if(result.length() > 0){
				result += "*";
			}
			result += string;
		}
		return result;
	}
	
}
